import api from '../../api/index'
import { AppId } from '../../api/config'
import { userInfoStore } from '../../store/userInfo'
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
var self;
const app = getApp();

Component({
  

  properties: {
    // 弹窗内容 
    
  },

  attached() {
    self=this;
    let { name, sex, province, city } = userInfoStore.userInfo
    this.setData({ name: name, sex: sex, province: province, city: city },()=>{
      self._checkComplete();
    })
  },

  detached() {},
  
  methods: {
    setAgree(val){
      let self= this
      this.setData({ agree:val },()=>{
        self._checkComplete();
      })
    },

    _showState(){
      this.triggerEvent("showstate")
    },

    _checkComplete: function () {
      if (this.data.name.length > 0 && this.data.agree && this.data.city !="") {
        this.setData({ enable: true });
      } else {
        this.setData({ enable: false });
      }
    },

    _selectSex: function (e) {
      this.setData({ sex: e.currentTarget.dataset.sex })
      this._checkComplete();
    },

    /**
     * 是否同意提交协议
     */
    _argeeCheck: function () {

      this.setData({
        agree: !this.data.agree
      });
      this._checkComplete();
    },

    _bindKeyInputName: function (e) {
      this.setData({
        name: e.detail.value
      })
    },

    _bindKeyNameBlur: function (e) {
      this._checkComplete();
    },

    _bindKeyInputCity: function (e) {
      this.setData({
        city: e.detail.value
      })
      console.log(this.data.city)
    },

    _bindKeyCityBlur: function (e) {
      // this._checkComplete();
    },

    _getPhoneNumber: (e) => {
      // console.log(e)
      if (e.detail.iv) {
        let { session_key } = userInfoStore.userInfo;
        let pc = new WXBizDataCrypt(AppId, session_key);
        var ret = pc.decryptData(e.detail.encryptedData, e.detail.iv)
        console.log('解密后 data: ', ret)

        var param = {
          openId: userInfoStore.userInfo.openId,
          name: self.data.name,
          sex: self.data.sex,
          province: self.data.province,
          city: self.data.city,
          phone: ret.phoneNumber,
          level: 1
        }

        if (app.globalData.inviteCode)
          param = Object.assign(param, { recomendCode: app.globalData.inviteCode })

        // console.log(param);
        api.saveUser(param).then(res => {
          let { userInfo } = userInfoStore;
          let { result } = res;
          result.session_key = userInfo.session_key;
          userInfoStore.updateUserInfo(result);
          wx.showModal({
            title: "提交成功",
            showCancel: false,
            confirmText: "我知道了"
          })
        }, err => {
          wx.showModal({
            title: "提交失败",
            content: err,
            showCancel: false,
            confirmText: "我知道了"
          })
        })
      }
    },
    _bindMultiPickerChange: function (e) {
      console.log('bindMultiPickerChange');
      console.log(e.detail.value);
      let selectValue = e.detail.value;
      this.setData({
        multiIndex: selectValue,
        province: this.data.multiArray[0][selectValue[0]],
        city: this.data.multiArray[1][selectValue[1]]
      })
      console.log(this.data);
      this._checkComplete();
    },
    _bindMultiPickerColumnChange: function (e) {
      switch (e.detail.column) {
        case 0:
          let list = []
          for (var i = 0; i < this.data.objectMultiArray.length; i++) {
            if (this.data.objectMultiArray[i].parid == this.data.objectMultiArray[e.detail.value].regid) {
              list.push(this.data.objectMultiArray[i].regname)
            }
          }
          console.log('bindMultiPickerColumnChange');
          console.log(e.detail.value);
          console.log(list);
          this.setData({
            "multiArray[1]": list,
            multiIndex: [e.detail.value, 0]
          })

      }
    }


    
  },

  data: {
    name: "",
    sex: 0,
    province: "",
    city: "",
    agree: false,
    enable: false,
    multiIndex: [0, 0],
    multiArray: [['北京', "天津", "河北", "山西", "其它"], ['北京']],
    objectMultiArray:
      [{ "regid": "2", "parid": "1", "regname": "北京", "regtype": "1", "ageid": "0" }, 
      { "regid": "27", "parid": "1", "regname": "天津", "regtype": "1", "ageid": "0" },
      { "regid": "10", "parid": "1", "regname": "河北", "regtype": "1", "ageid": "0" }, 
      { "regid": "23", "parid": "1", "regname": "山西", "regtype": "1", "ageid": "0" }, 
        { "regid": "9999", "parid": "1", "regname": "其它", "regtype": "1", "ageid": "0" },
      { "regid": "52", "parid": "2", "regname": "北京", "regtype": "2", "ageid": "0" },
      { "regid": "343", "parid": "27", "regname": "天津", "regtype": "2", "ageid": "0" },
      { "regid": "138", "parid": "10", "regname": "石家庄", "regtype": "2", "ageid": "0" },
      { "regid": "146", "parid": "10", "regname": "唐山", "regtype": "2", "ageid": "0" },  
      { "regid": "142", "parid": "10", "regname": "邯郸", "regtype": "2", "ageid": "0" },
      { "regid": "145", "parid": "10", "regname": "秦皇岛", "regtype": "2", "ageid": "0" },  
      { "regid": "139", "parid": "10", "regname": "保定", "regtype": "2", "ageid": "0" }, 
      { "regid": "148", "parid": "10", "regname": "张家口", "regtype": "2", "ageid": "0" },
      { "regid": "141", "parid": "10", "regname": "承德", "regtype": "2", "ageid": "0" }, 
      { "regid": "144", "parid": "10", "regname": "廊坊", "regtype": "2", "ageid": "0" }, 
      { "regid": "140", "parid": "10", "regname": "沧州", "regtype": "2", "ageid": "0" }, 
      { "regid": "143", "parid": "10", "regname": "衡水", "regtype": "2", "ageid": "0" }, 
      { "regid": "147", "parid": "10", "regname": "邢台", "regtype": "2", "ageid": "0" }, 
      { "regid": "300", "parid": "23", "regname": "太原", "regtype": "2", "ageid": "0" }, 
      { "regid": "302", "parid": "23", "regname": "大同", "regtype": "2", "ageid": "0" },
      { "regid": "309", "parid": "23", "regname": "阳泉", "regtype": "2", "ageid": "0" },  
      { "regid": "301", "parid": "23", "regname": "长治", "regtype": "2", "ageid": "0" }, 
      { "regid": "304", "parid": "23", "regname": "晋中", "regtype": "2", "ageid": "0" },
      { "regid": "303", "parid": "23", "regname": "晋城", "regtype": "2", "ageid": "0" }, 
      { "regid": "305", "parid": "23", "regname": "临汾", "regtype": "2", "ageid": "0" },
      { "regid": "310", "parid": "23", "regname": "运城", "regtype": "2", "ageid": "0" }, 
      { "regid": "307", "parid": "23", "regname": "朔州", "regtype": "2", "ageid": "0" }, 
      { "regid": "308", "parid": "23", "regname": "忻州", "regtype": "2", "ageid": "0" }, 
      { "regid": "306", "parid": "23", "regname": "吕梁", "regtype": "2", "ageid": "0" },
      { "regid": "10000", "parid": "9999", "regname": "其它", "regtype": "2", "ageid": "0" }
      ]
  } 

})